<?php

namespace My\Controller;

use Utuo\BackgroundController;

class WechatMaterialController extends BackgroundController {
	
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('素材管理');
		$this->setActionTitle(array(
				'index' => '素材列表',
				'add' => '添加素材',
				'recycle' => '回收站',
				'modify' => '修改素材' 
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle' 
		));
	}
	
	// 素材列表
	public function indexAction() {
		$this->getMaterialList(0);
	}
	
	// 获取列表
	private function getMaterialList($is_deleted) {
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$d = D('WechatMaterial');
		$condition = array(
				'is_deleted' => $is_deleted,
				'cid' => $this->aid 
		);
		$materialLists = $d->where($condition)->limit($pagesize)->page($page)->order('id DESC')->select();
		$materialCount = $d->where($condition)->count();
		foreach ($materialLists as &$v){
			$v['typeName'] = $d->materialTypes[$v['type']];
		}
		$this->assign('materialLists', $materialLists);
		$this->showPage($materialCount, $pagesize);
		$this->display();
	}
	
	// 添加素材页面
	public function addAction() {
		$typeOptions = $this->getOption(D('WechatMaterial')->materialTypes);
		$this->assign('typeOptions', $typeOptions);
		$this->display();
	}
	
	// 添加素材
	public function addPostAction() {
		$d = D('WechatMaterial');
		if ($d->create()) {
			$d->cid = $this->aid;
			$d->news = htmlspecialchars_decode($d->news);
			if ($d->add()) {
				$this->ajaxSuccess('添加素材成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}
	
	// 修改页面
	public function modifyAction() {
		$id = I('id', 0, 'intval');
		$d = D('WechatMaterial');
		$material = $d->find($id);
		$material['typeOptions'] = $this->getOption($d->materialTypes, $material['type']);
		$this->assign('material', $material);
		$this->display();
	}
	
	// 提交修改
	public function modifyPostAction() {
		$d = D('WechatMaterial');
		if ($d->create()) {
			$d->cid = $this->aid;
			$d->news = htmlspecialchars_decode($d->news);
			if (false !== $d->save()) {
				$this->ajaxSuccess('修改素材成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 文件上传
	public function uploadAction() {
		$upload = new \Utuo\Upload();
		$info = $upload->uploadOne();
		if (!$info) {
			$this->ajaxError($upload->getError());
		} else {
			$this->ajaxSuccess("上传成功", $info);
		}
	}
	
	// 移除菜单
	public function removePostAction() {
		$this->ajaxRemove();
	}
	
	// 恢复菜单
	public function restorePostAction() {
		$this->ajaxRestore();
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete();
	}
	
	// 回收站
	public function recycleAction() {
		$this->getMaterialList(1);
	}
	
	// 获得素材列表
	public function getNewsListPostAction() {
		$d = D('WechatMaterial');
		$materialLists = $d->getNewsList($this->aid);
		if ($materialLists || !$d->getError()) {
			$this->ajaxSuccess('', $materialLists);
		} else {
			$this->ajaxError("获取图文列表失败");
		}
	}
}